﻿using System.Security.Cryptography;

namespace WorldmeServer.Utils
{
    public class RandomUtils
    {

        public static string GenerateNumble(int length)
        {
            Random random = new Random();
            string code = "";

            for (int i = 0; i < length; i++)
            {
                int digit = random.Next(0, 10); // 生成0到9之间的随机数
                code += digit.ToString();
            }

            return code;
        }
        public static string GenerateUid()
        {
            return GenerateNumble(10);
        }

        public static string GenerateCode()
        {
            return GenerateNumble(6);
        }
        public static string GenerateSalt(int length = 16)
        {
            // 创建一个字节数组来存储盐值
            byte[] salt = new byte[length];

            // 使用 RandomNumberGenerator 生成随机字节
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }

            return Convert.ToBase64String(salt);
        }
    }
}
